package com.ear.orange.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ear.orange.common.CustomException;
import com.ear.orange.common.Result;
import com.ear.orange.entity.Category;
import com.ear.orange.entity.Dish;
import com.ear.orange.entity.Setmeal;
import com.ear.orange.mapper.CategoryMapper;
import com.ear.orange.service.CategoryService;
import com.ear.orange.service.DishService;
import com.ear.orange.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author orange
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Resource
    private CategoryMapper categoryMapper;

    /**
     * 根据id删除分类，删除之间需要进行判断
     *
     * @param id 不知道什么id
     */
    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件，根据分类id进行查询
        dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
        int count1 = dishService.count(dishLambdaQueryWrapper);

        //查询当前分类是否关联了菜品。如果已经关联，抛出一个业务异常
        if (count1 > 0) {
            //已经关联菜品，抛出一个业务异常
            throw new CustomException("当前分类下关联了菜品，不能删除");
        }

        //查询当前分类是否关联了套餐。如果已经关联，抛出一个业务异常
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件，根据分类id进行查询
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
        int count2 = setmealService.count(setmealLambdaQueryWrapper);
        if (count2 > 0) {
            //已经关联套餐，抛出一个业务异常
            throw new CustomException("当前分类下关联了套餐，不能删除");
        }

        //正常删除分类
        super.removeById(id);
    }

    /**
     * 新增分类
     *
     * @param category 分类
     * @return 封装类
     */
    @Override
    public Result saveCategory(Category category) {
        int insert = categoryMapper.insert(category);
        if (insert == 1) {
            return Result.success(Result.INSERT_SUCCESS, category);
        }
        return Result.error(Result.INSERT_ERROR);
    }

    /**
     * 分页查询
     *
     * @param page     当前页
     * @param pageSize 每页显示条数
     * @return 封装结果
     */
    @Override
    public Result<Page<Category>> pageInfo(int page, int pageSize) {
        //分页构造器
        Page<Category> pageInfo = new Page<>(page, pageSize);
        //条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        //添加排序条件，根据sort进行排序
        queryWrapper.orderByAsc(Category::getSort);

        //进行分页查询
        page(pageInfo, queryWrapper);

        return Result.success("第" + page + "页数据", pageInfo);
    }

    /**
     * 根据id删除分类信息
     *
     * @param id 分类id
     * @return 结果
     */
    @Override
    public Result<String> deleteCategory(Long id) {
        int delete = categoryMapper.deleteById(id);
        if (delete == 1) {
            return Result.success(Result.DELETE_SUCCESS);
        }
        return Result.error(Result.DELETE_ERROR);
    }

    /**
     * 根据id修改分类信息
     *
     * @param category 分类对象
     * @return 结果
     */
    @Override
    public Result updateCategoryById(Category category) {
        int update = categoryMapper.updateById(category);
        if (update == 1) {
            return Result.success(Result.UPDATE_SUCCESS, category);
        }
        return Result.error(Result.UPDATE_ERROR);
    }

    /**
     * 根据条件查询分类数据
     *
     * @param category 分类
     * @return 结果
     */
    @Override
    public Result<List<Category>> pageList(Category category) {
        //条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        queryWrapper.eq(category.getType() != null, Category::getType, category.getType());
        //添加排序条件
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        List<Category> list = categoryMapper.selectList(queryWrapper);
        return Result.success(Result.SELECT_SUCCESS, list);
    }
}
